package com.leaf.u_jvm;

import java.util.ArrayList;
import java.util.List;

/**
 * VMargs: -Xms100m -Xmx100m -XX:+UseSerialGC
 * 
 * 代码以64k/50毫秒的速度往Java堆中填充数据，使用jconsole的内存页签进行监视，观察曲线和柱状指示图的变化
 *
 */
public class OOMDemo {

	static class OOMObject{
		public byte[] placeholder = new byte[64 * 1024];
	}
	
	public static void fillHeap(int num) throws InterruptedException{
		List<OOMObject> list = new ArrayList<OOMDemo.OOMObject>();
		for(int i = 0; i<num; i++){
			//稍作延时，令监视曲线的变化更加明显
			Thread.sleep(50);
			
			list.add(new OOMObject());
		}
		System.gc();
	}
	
	public static void main(String[] args) throws InterruptedException {
		fillHeap(1000);
	}
	
}
